<template>
  <div class="featured">
    <div class="featured-container">
      <div v-if="datas.form.source_list.length > 0" class="featured-header">
        <div class="title">{{ datas.form.source_list[0].label }}</div>
        <div class="category">
          <span
            v-for="(item, index) in datas.form.source_list[0].children.slice(0,5)"
            :key="index"
            class="li"
            :class="{ active: index == active }"
            @click="onClick(index)"
          >
            {{ item.label.substring(0, 5) }}
          </span>
        </div>
        <div class="more">更多<i class="el-icon-arrow-right right"></i></div>
      </div>
      <div v-else class="featured-header">
        <div class="title"></div>
        <div class="category"></div>
        <div class="more">更多<i class="el-icon-arrow-right right"></i></div>
      </div>
      <div class="featured-body">
        <div class="featured-body__advert">
          <!--<el-image :src="advertImg">-->
          <!--  <div slot="error" class="image-error">-->
          <!--    <img-->
          <!--      class="image-error__icon"-->
          <!--      src="@/assets/images/template/placeholder.png"-->
          <!--    />-->
          <!--  </div>-->
          <!--</el-image>-->
          <div class="advert-title">{{ datas.form.source_list[0].label }}</div>
          <div class="advert-line"></div>
          <div class="advert-course">
            <el-image :src="advertImg" :height="89"></el-image>
            <div class="content">
              <div class="title">{{ datas.form.advert_course_title }}</div>
              <div class="parallel">
                <div class="num">123 人报名</div>
                <div class="price">¥99.99</div>
              </div>
            </div>
          </div>
        </div>
        <div class="featured-body__list">
          <div v-for="(item, index) in getMockData()" :key="index" class="item">
            <div class="image">
              <el-image :src="item.cover_url">
                <div slot="error" class="image-error">
                  <img
                    class="image-error__icon"
                    src="@/assets/images/template/placeholder.png"
                  />
                </div>
              </el-image>

              <!-- 活动图标 -->
              <img
                class="activity-icon"
                src="@/assets/images/template/pc/course_discount_icon.png"
              />

              <!-- tag -->
              <img
                v-if="item.course_type === 2"
                class="tag-icon"
                src="@/assets/images/template/pc/course_live_icon.png"
              />
              <img
                v-else-if="item.course_type === 4"
                class="tag-icon"
                src="@/assets/images/template/pc/course_class_icon.png"
              />
            </div>
            <div class="desc">
              <div class="title">{{ item.title }}</div>
              <div class="bottom">
                <div class="number">{{ item.num }}人报名</div>
                <div class="price">¥{{ item.price }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 删除组件 -->
    <slot name="deles" />
  </div>
</template>

<script>
export default {
  name: 'Featured',
  props: {
    datas: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      active: 0
    }
  },
  computed: {
    advertImg() {
      return this.datas.form.advert_url ? this.datas.form.advert_url : require('@/assets/images/template/course_3.png')
    }
  },
  watch: {
    'datas.form.source_index': {
      handler() {
        this.setAll()
      }
    }
  },
  created() {
    this.setAll()
  },
  methods: {
    onClick(index) {
      this.active = index
    },
    setAll() {
      if (this.datas.form.source === 1) {
        const sourceList = []
        for (let i = 0; i < 5; i++) {
          sourceList.push({
            label: '二级分类',
            value: i + 1
          })
        }
        // this.categoryOptions.forEach(item => {
        //   sourceList.push({
        //     label: item.label,
        //     value: item.value
        //   })
        // })
        this.datas.form.source_list = [
          { label: '一级分类', value: 0, children: sourceList }
        ]
      }
    },
    // 课程模拟数据
    getMockData() {
      const mockData = []
      const course = [
        {
          title: '示例点播课程',
          course_type: 1,
          num: 123,
          price: 99.99,
          cover_url: ''
        },
        {
          title: '示例直播课程',
          course_type: 2,
          num: 123,
          price: 99.99,
          cover_url: ''
        },
        {
          title: '示例班级课程',
          course_type: 4,
          num: 123,
          price: 99.99,
          cover_url: ''
        }
      ]
      for (let i = 0; i < 8; i++) {
        const k = i % 3
        const item = { ...course[k] }
        item.cover_url = require('@/assets/images/template/course_' +
            ((i % 4) + 1) +
            '.png')
        mockData.push(item)
      }

      return mockData
    }
  }
}
</script>

<style scoped lang="scss">
@import '@/styles/mixin.scss';
.featured {
  padding: 15px 0;
  position: relative;

  &-container {
    width: 834px;
    margin: 0 auto;
    background: #ffffff;
  }

  &-header {
    display: flex;
    align-items: center;
    height: 62px;
    padding: 0 15px;

    .title {
      font-size: 17px;
      font-weight: 500;
      font-weight: bold;
      color: #333333;
      line-height: 24px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      max-width: 80px;
    }

    .category {
      flex: 1;
      min-width: 0;

      .li {
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        line-height: 20px;
        margin-left: 40px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 80px;
        position: relative;

        &.active {
          color: #5191ff;
          font-weight: 500;

          &::after {
            content: '';
            width: 26px;
            height: 3px;
            background: #5191ff;
            border-radius: 2px;
            position: absolute;
            left: 50%;
            bottom: -10px;
            margin-left: -13px;
          }
        }
      }
    }

    .more {
      font-size: 12px;
      font-weight: 400;
      color: #888888;
      line-height: 16px;
    }
  }

  &-body {
    padding: 0 15px 20px;
    display: grid;
    grid-column-gap: 15px;
    grid-template-columns: 150px 1fr;

    &__advert {
      //height: 390px;
      padding: 26px 13px 0;
      background: #A9C8FF;
      .advert-title {
        font-size: 18px;
        font-weight: bold;
        color: #ffffff;
        line-height: 26px;
        @include ellipsis;
      }
      .advert-line {
        margin: 10px 0;
        width: 30px;
        height: 1px;
        background: #ffffff;
      }
      .advert-course {
        padding: 14px 10px 20px;
        background: #ffffff;
        border-radius: 2px;
        .content {
          margin-top: 10px;
          .title {
            font-size: 14px;
            font-weight: 500;
            color: #303133;
            cursor: pointer;
            @include ellipsisLine(2);
          }
          .parallel {
            margin-top: 12px;
            display: flex;
            align-items: center;
            .num {
              flex: 1;
              min-width: 0;
              font-size: 12px;
              font-weight: 400;
              color: #909399;
            }
            .price,
            .free {
              font-size: 12px;
              font-weight: 500;
              margin-left: 5px;
            }
            .price {
              color: #f54030;
            }
            .free {
              color: #67c23a;
            }
          }
        }
      }
    }
    //
    //&__advert {
    //  width: 100%;
    //  height: 285px;
    //
    //  .el-image {
    //    width: 100%;
    //    height: 100%;
    //
    //    ::v-deep.image-error {
    //      width: 100%;
    //      height: 100%;
    //      display: flex;
    //      align-items: center;
    //      justify-content: center;
    //      width: 100%;
    //      height: 100%;
    //      background-color: #f1f1f1;
    //
    //      .image-error__icon {
    //        width: 40px;
    //        height: 28px;
    //      }
    //    }
    //  }
    //}

    &__list {
      display: grid;
      grid-gap: 10px;
      grid-template-columns: repeat(4, 1fr);

      .item {
        .image {
          position: relative;
          height: 86px;

          .el-image {
            width: 100%;
            height: 100%;

            ::v-deep.image-error {
              width: 100%;
              height: 100%;
              display: flex;
              align-items: center;
              justify-content: center;
              width: 100%;
              height: 100%;
              background-color: #f1f1f1;
              border-radius: 6px;

              .image-error__icon {
                width: 40px;
                height: 28px;
              }
            }
          }

          .activity-icon {
            position: absolute;
            right: 0;
            bottom: 0;
          }

          .tag-icon {
            position: absolute;
            left: 0;
            top: 0;
          }
        }

        .desc {
          margin-top: 8px;
        }

        .title {
          font-size: 14px;
          font-weight: 500;
          color: #333333;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
        }

        .bottom {
          margin-top: 8px;
          display: flex;
          align-items: center;
          justify-content: space-between;
        }

        .number {
          font-size: 12px;
          font-weight: 400;
          color: #888888;
        }

        .price {
          font-size: 14px;
          font-weight: 500;
          color: #f54030;
        }

        .free {
          font-size: 14px;
          font-weight: 500;
          color: #67c23a;
        }
      }
    }
  }
}
</style>
